<?php
defined('BASEPATH') OR exit('No direct script access allowed');

//卡
class CourseCard extends MY_Controller {

	function __construct(){
		
		parent::__construct();
	}	
	
	//start
	/**
	* @name 添加卡
	* @param name true char 名称
	* @param course_id true char 课程ID
	* @param price true char 价格
	* @param course_num true char 次数
	* @param brief true char 介绍
	* @return 
	*/
	//end
	function add(){
		$user_id = $this->user_id;
		
		$arr = array(
			'card_name' => $this->input->post('name',null,''),
			'course_id' => $this->input->post('course_id',null,0),
			'price' => $this->input->post('price',null,0),
			'course_num' => $this->input->post('course_num',null,10),
			'brief' => $this->input->post('brief',null,''),
		);
		
		$ver_arr = array(
			'card_name'=>"名称不能为空",
			'course_id'=>"课程不能为空",
			'price'=>"价格不能为空",
			'course_num'=>"数量不能为空",
			'brief'=>"介绍不能为空",
		);
		
		$result = $this->CheckData($ver_arr,$arr);
		if($result){
			$this->err_out($result);
		}
		
		if($arr['course_num']<=0){
			$this->err_out("使用次数不能小于或等于0");
		}
		if($arr['price']<=0){
			$this->err_out("价格不能小于或等于0");
		}
		
		
		$this->load->model('Sport/CourseModel','Course');
		$info = $this->Course->get_info($user_id,$arr['course_id']);
		if(!$info){
			$this->err_out('课程不存在');
		}
		
		
		$this->load->model('Sport/CourseCardModel','CourseCard');
		
		
		$insert_id = $this->CourseCard->add($arr);
		$arr['id'] = $insert_id;
		
		$this->data_out($arr);
	}
	
	//start
	/**
	* @name 根据课程获取卡
	* @param course_id true char 课程ID
	* @return 
	*/
	//end
	function get_card(){
		$user_id = $this->user_id;
		$cid = $this->input->post('course_id',null,0);
		
		$this->load->model('Sport/CourseModel','Course');
		$info = $this->Course->get_info($user_id,$cid);
		if(!$info){
			$this->err_out('课程不存在');
		}
		
		$this->load->model('Sport/CourseCardModel','CourseCard');

		$list = $this->CourseCard->course_get_list($cid);
		
		$this->data_out($list);
	}
}
